//
//  ViewController.swift
//  C013_action_圆形图片加载动画
//
//  Created by yao_yu on 15/6/13.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var progressIndicatorView: CircularLoaderView!
    
    var viewForLayer = UIView(frame: CGRectZero)
    var l:CALayer {
        return viewForLayer.layer
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        view.addSubview(viewForLayer)
        setUpLayer()
        
        viewForLayer.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: "pinchGestureRecognized:"))
        viewForLayer.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tapGestureRecognized:"))
        
        progressIndicatorView = CircularLoaderView(frame: CGRectMake(0, 0, 300, 600))
        view.addSubview(progressIndicatorView)
        
        
    }
    
    func setUpLayer() {
        let asize:CGFloat = 200
        viewForLayer.frame = CGRectMake( (view.frame.width - asize) / 2, 30 ,  asize, asize)
        l.backgroundColor = UIColor.blueColor().CGColor
        l.borderWidth = 60.0
        l.borderColor = UIColor.redColor().CGColor
        l.shadowOpacity = 0.7
        l.shadowRadius = 10.0
        
        let img:UIImage? = UIImage(named: "3droad_a_l")
        l.contents = img?.CGImage
        l.contentsGravity = kCAGravityCenter
        l.contentsScale = img!.size.width / asize
        
        l.magnificationFilter = kCAFilterLinear
        l.geometryFlipped = false
        
        l.opacity = 1.0
        l.hidden = false
        l.masksToBounds = false
    }
    
    override func viewDidLayoutSubviews() {
        viewForLayer.frame = CGRectMake( (view.frame.width -  viewForLayer.frame.width) / 2, 30,  viewForLayer.frame.width, viewForLayer.frame.width)
        progressIndicatorView.frame =  CGRectMake( (view.frame.width -  viewForLayer.frame.width) / 2, viewForLayer.frame.width + 100,  viewForLayer.frame.width, viewForLayer.frame.width)
        print(progressIndicatorView.frame)

//        viewForLayer.frame = CGRectMake( (view.frame.width -  viewForLayer.frame.width) / 2, (view.frame.height -  viewForLayer.frame.height) / 2 ,  viewForLayer.frame.width, viewForLayer.frame.width)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func pinchGestureRecognized(sender: UIPinchGestureRecognizer) {
        let offset: CGFloat = sender.scale < 1 ? 5.0 : -5.0
        let oldFrame = l.frame
        let oldOrigin = oldFrame.origin
        let newOrigin = CGPoint(x: oldOrigin.x + offset, y: oldOrigin.y + offset)
        let newSize = CGSize(width: oldFrame.width + (offset * -2.0), height: oldFrame.height + (offset * -2.0))
        let newFrame = CGRect(origin: newOrigin, size: newSize)
        if newFrame.width >= 100.0 && newFrame.width <= 300.0 {
            l.borderWidth -= offset
            l.cornerRadius += (offset / 2.0)
            l.frame = newFrame
        }
    }
    
    func tapGestureRecognized(sender: UITapGestureRecognizer) {
        l.shadowOpacity = l.shadowOpacity == 0.7 ? 0.0 : 0.7
        if progressIndicatorView.progress < 1 {
            progressIndicatorView.progress += 1
        } else  {
            progressIndicatorView.reveal()
            //progressIndicatorView.progress = 0
//            progressIndicatorView.removeFromSuperview()
        }
    }
    
}

